package com.gfhd.feign.config;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//@Configuration
public class WebConfiguration {
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        RestTemplate rt = builder
                //设置连接超时时间
                .setConnectTimeout(Duration.ofSeconds(5000))
                //设置读取超时时间
                .setReadTimeout(Duration.ofSeconds(5000))
                //设置认证信息
                //.basicAuthentication("username","password")
                //设置根路径
                //.rootUri("https://api.test.com/")
                //构建
                .build();
        /*List<HttpMessageConverter<?>> messageConverters = rt.getMessageConverters();
        for (HttpMessageConverter<?> messageConverter : messageConverters) {
            if(messageConverter instanceof MappingJackson2HttpMessageConverter){
                ((MappingJackson2HttpMessageConverter) messageConverter).setSupportedMediaTypes(
                        Arrays.asList(MediaType.TEXT_HTML,MediaType.TEXT_PLAIN)
                );
            }
        }*/
        return rt;
    }
}
